function [Return,Z,IsF,Nt,Chars,CharsNames,time_beg,time_end,DivDummy] = package_data_all_greeks

load('../data/delta_call');

%-----
%This file has data with alignement such as Chars are as of time_beg and Returns are as of time_end
%All data are 323*1 (it is not divisible by 12 because we do not have return for the first month Jan 1996)
%time_beg goes from 1996/01/22 to 2022/11/21
%time_end goes from 1996/02/22 to 2022/12/16
%-----

%% allign Returns and Chars for IPCA code

%----
%IPCA routine expects the alignment to be "end of period"
%Meaning that all T*1 vectors are end of period
%-----

%---
%Add one more time period to make the data from Jan 1996 to Dec 2022
%---
[N,T,L] = size(Chars);
time_end = [time_beg(1)-3;time_end];
time_beg = [19951222;time_beg];

% move returns over by 1 column and subtract Rf
% since Returns are originally as of time_end, add NaN at the beginning for the month of Jan 1996
rets = fieldnames(Return);
for i = 1:size(rets,1)
    xx = getfield(Return,rets{i,1});

    % need to line up by date
    xx = [NaN(N,1) xx-repmat(Rf',N,1)];
    Return = setfield(Return,rets{i,1},xx);
end

% add one column of NaN at the end of Chars
% since chars are originally as of time_beg, add NaN at the end for the month of Dec 2022
chars1 = NaN(N,T+1,L);
for l = 1:L
    chars1(:,:,l) = [squeeze(Chars(:,:,l)) NaN(N,1)];
end
Chars = chars1; clear chars1
[N,T,L] = size(Chars);

% create filter for NaN across all Chars and Return
IsF_Chars = all(isfinite(Chars),3);
IsF_R = isfinite(Return.ret_daily);

IsF = false(N,T);
IsF(:,2:end) = IsF_R(:,2:end);
clear IsF_Chars IsF_R;

Nt = sum(IsF)';
L = size(Chars,3) + 1; % add 1 for constant

% convert characteristics into ranks
Z = NaN(N,T,L,'single');
for t = 1:T-1
    Ct = squeeze(Chars(:,t,:));
    for l = 1:L-1
        Ctl = Ct(:,l);
        IsC = isfinite(Ctl);
        CtlInclude = Ctl(IsC);

        % assign the same rank in case of duplicates
        [~,~,ia] = unique(CtlInclude);
        RnkInclude = ia/(max(ia)+1);

        % normal inverse
        RnkInclude = norminv(RnkInclude,0,1);
                
        Z(IsC,t,l) = RnkInclude;
    end
end

Z(:,:,L) = 1; % last char is a constant

% pre-fill all missing observations to 0
Z(~isfinite(Z(:))) = 0;
